package com.yourcompany.web.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.yourcompany.api.dto.UserLoginDTO;
import com.yourcompany.api.dto.WxLoginDTO;
import com.yourcompany.api.service.UserService;
import com.yourcompany.api.strategy.LoginStrategy;
import com.yourcompany.service.factory.LoginStrategyFactory;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Tag(name = "认证管理")
@RestController
@RequestMapping("/api/auth")
public class AuthController {

    @Autowired
    private UserService userService;

    @Autowired
    private LoginStrategyFactory loginStrategyFactory;

    @Operation(summary = "登录")
    @PostMapping("/login")
    public SaResult login(@Validated @RequestBody UserLoginDTO loginDTO) {
        String token = userService.login(loginDTO);
        return SaResult.ok().setData(token);
    }

    @Operation(summary = "登出")
    @PostMapping("/logout")
    public SaResult logout() {
        StpUtil.logout();
        return SaResult.ok();
    }

    @Operation(summary = "微信登录")
    @PostMapping("/wx/login")
    public SaResult wxLogin(@Validated @RequestBody WxLoginDTO loginDTO) {
        LoginStrategy strategy = loginStrategyFactory.getStrategy(loginDTO.getLoginType());
        return SaResult.ok().setData(strategy.login(loginDTO));
    }
} 